﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class AreaDetailBLL : IBaseInfoDetail
    {
        private static readonly IAreaDetailDA areaDetailDA = DataAccess.CreateAreaDetailDA();
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private PubParasInfo pubParasInfo;
        private static readonly IZoneDA zoneDA = DataAccess.CreateZoneDA();

        public AreaDetailBLL()
        {
            this.pubParasInfo = null;
        }

        public AreaDetailBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public AreaDetailBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string DeleteDetail(List<string> mainID, List<string> subId, List<string> pk3)
        {
            string temp = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                int i;
                if (mainID.Count != subId.Count)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F2340_004", this.pubParasInfo.UserLanguage);
                }
                List<AreaDetailInfo> areaDetailInfos = new List<AreaDetailInfo>();
                AreaDetailInfo areaDetailInfo = null;
                for (i = 0; i < mainID.Count; i++)
                {
                    areaDetailInfo = areaDetailDA.GetAreaDetailByID(dataBase, tran, mainID[i], subId[i], this.dataBaseID[0]);
                    areaDetailInfos.Add(areaDetailInfo);
                }
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                if (areaDetailDA.DeleteAreaDetail(dataBase, tran, mainID, subId, this.dataBaseID) > 0)
                {
                    for (i = 0; i < mainID.Count; i++)
                    {
                        auditDataBLL.InsertAuditData<AreaDetailInfo>(dataBase, tran, areaDetailInfos[i], null, AuditAction.Delete, this.pubParasInfo.UserID, "AREA_DETAIL", this.dataBaseID[0]);
                        string CS00002 = temp;
                        temp = CS00002 + mainID[i] + "," + subId[i] + ";";
                    }
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            if (temp.Length > 0)
            {
                return temp.Substring(0, temp.Length - 1);
            }
            return "";
        }

        public DataSet GetAllDetailInfo()
        {
            return areaDetailDA.GetAllAreaDetail(this.dataBaseID[0]);
        }

        public DataSet GetDetaiInfolByQueryEntity(object obj)
        {
            AreaQueryEntity areaQuery = obj as AreaQueryEntity;
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(areaQuery.AreaID))
            {
                sqlWhere.Add(" AREA_ID " + areaQuery.AreaID);
            }
            return areaDetailDA.GetAreaDetailByQueryList(sqlWhere, areaQuery, this.dataBaseID[0]);
        }

        public object GetDetailInfoByID(string mainID, string subID, string pk3)
        {
            return areaDetailDA.GetAreaDetailByID(mainID, subID, this.dataBaseID[0]);
        }

        public List<object> GetDetailInfoListByID(string mainID, string subID)
        {
            List<AreaDetailInfo> aredDetails = areaDetailDA.GetAreaDetailByAreaID(mainID, this.dataBaseID[0]);
            List<object> objs = null;
            if ((aredDetails != null) && (aredDetails.Count > 0))
            {
                objs = new List<object>();
                for (int i = 0; i < aredDetails.Count; i++)
                {
                    objs.Add(aredDetails[i]);
                }
            }
            return objs;
        }

        public string InsertDetail(object obj)
        {
            AreaDetailInfo areaDetailInfo = obj as AreaDetailInfo;
            string areaDetailID = string.Empty;
            try
            {
                if (!zoneDA.CheckZoneIDUnique(areaDetailInfo.ZoneID, this.dataBaseID[0]))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F2340_003", this.pubParasInfo.UserLanguage, new List<string> { areaDetailInfo.ZoneID });
                }
                if (areaDetailDA.CheckAreaDetailIDUnique(areaDetailInfo.AreaID, areaDetailInfo.ZoneID, this.dataBaseID[0]))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F2340_002", this.pubParasInfo.UserLanguage, new List<string> { areaDetailInfo.AreaID, areaDetailInfo.ZoneID });
                }
                if (areaDetailInfo == null)
                {
                    return areaDetailID;
                }
                areaDetailInfo.UpdatedBy = areaDetailInfo.CreatedBy;
                areaDetailInfo.CreatedDate = DateTime.Now;
                areaDetailInfo.UpdatedDate = DateTime.Now;
                if (areaDetailDA.InsertAreaDetail(areaDetailInfo, this.dataBaseID) > 0)
                {
                    new AuditDataBLL().InsertAuditData<AreaDetailInfo>(null, areaDetailInfo, AuditAction.Insert, this.pubParasInfo.UserID, "AREA_DETAIL", this.dataBaseID[0]);
                    areaDetailID = areaDetailInfo.AreaID;
                }
            }
            catch (Exception ex)
            {
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            return areaDetailID;
        }

        public string UpdateDetail(object obj)
        {
            string areaDetailID = string.Empty;
            try
            {
                AreaDetailInfo areaDetail = obj as AreaDetailInfo;
                AreaDetailInfo oldAreaDetailInfo = areaDetailDA.GetAreaDetailByID(areaDetail.AreaID, areaDetail.ZoneID, this.dataBaseID[0]);
                if (areaDetailDA.UpdateAreaDetail(areaDetail, this.dataBaseID) > 0)
                {
                    new AuditDataBLL().InsertAuditData<AreaDetailInfo>(oldAreaDetailInfo, areaDetail, AuditAction.Update, this.pubParasInfo.UserID, "AREA_DETAIL", this.dataBaseID[0]);
                    areaDetailID = areaDetail.AreaID + "," + areaDetail.ZoneID;
                }
            }
            catch (Exception ex)
            {
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            return areaDetailID;
        }

        public string UpdateIsActive(List<string> mainID, List<string> subId, string isActive)
        {
            throw new NotImplementedException();
        }
    }
}

